package cn.wh.xwm.hibernate.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Inventoryinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see cn.wh.xwm.hibernate.dao.Inventoryinfo
 * @author MyEclipse Persistence Tools
 */

public class InventoryinfoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(InventoryinfoDAO.class);
	// property constants
	public static final String INVENTORY_NUMBER = "inventoryNumber";

	public void save(Inventoryinfo transientInstance) {
		log.debug("saving Inventoryinfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Inventoryinfo persistentInstance) {
		log.debug("deleting Inventoryinfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Inventoryinfo findById(cn.wh.xwm.hibernate.dao.InventoryinfoId id) {
		log.debug("getting Inventoryinfo instance with id: " + id);
		try {
			Inventoryinfo instance = (Inventoryinfo) getSession().get(
					"cn.wh.xwm.hibernate.dao.Inventoryinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Inventoryinfo instance) {
		log.debug("finding Inventoryinfo instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.wh.xwm.hibernate.dao.Inventoryinfo").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Inventoryinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Inventoryinfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByInventoryNumber(Object inventoryNumber) {
		return findByProperty(INVENTORY_NUMBER, inventoryNumber);
	}

	public List findAll() {
		log.debug("finding all Inventoryinfo instances");
		try {
			String queryString = "from Inventoryinfo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Inventoryinfo merge(Inventoryinfo detachedInstance) {
		log.debug("merging Inventoryinfo instance");
		try {
			Inventoryinfo result = (Inventoryinfo) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Inventoryinfo instance) {
		log.debug("attaching dirty Inventoryinfo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Inventoryinfo instance) {
		log.debug("attaching clean Inventoryinfo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 通过ID查询库存
	 * @param customerId
	 * @return
	 */
	public List<Inventoryinfo> findByCustomerId(String customerId) {
		log.debug("findByCustomerId");
		try {
			String queryString = "from Inventoryinfo where id.customerId = '"+customerId+"'";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("findByCustomerId", re);
			throw re;
		}
	}
	
	/**
	 * 增加库存
	 * @param customerId
	 * @param productId
	 * @param amount
	 */
	public void increaseInventory(String customerId,String productId,String amount){
		log.debug("addInventory");
		try {
			InventoryinfoId id = new InventoryinfoId();
			id.setCustomerId(customerId);
			id.setProductId(productId);
			Inventoryinfo info = this.findById(id);
			info.setInventoryNumber(info.getInventoryNumber()+Integer.valueOf(amount));
		} catch (RuntimeException re) {
			log.error("addInventory", re);
			throw re;
		}
	}
	/**
	 * 减少库存
	 * @param customerId
	 * @param productId
	 * @param amount
	 */
	public void reduceInventory(String customerId,String productId,String amount){
		log.debug("reduceInventory");
		try {
			InventoryinfoId id = new InventoryinfoId();
			id.setCustomerId(customerId);
			id.setProductId(productId);
			Inventoryinfo info = this.findById(id);
			info.setInventoryNumber(info.getInventoryNumber()-Integer.valueOf(amount));
		} catch (RuntimeException re) {
			log.error("reduceInventory", re);
			throw re;
		}
	}
}